﻿using DBModels;
using Models.RModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Method.DAL
{
    public class GetUserInfoByUid
    {
        /// <summary>
        /// 获取用户基础信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public R_UserInfo GetUserInfo(int uid)
        {
            var infoR = new RedisHelper.User.UserInfo().GetUserInfo(uid);
            if (infoR is null)
            {
                using (ArticeContext context = new ArticeContext())
                {
                    var infoDB = context.UserInfos.FirstOrDefault(u => u.Id == uid);
                    if (infoDB is null)
                    {
                        return null;
                    }
                    else
                    {
                        R_UserInfo r_UserInfo = new R_UserInfo() { 
                            Avatar = infoDB.Avatar,
                            NickName = infoDB.NickName,
                            Level = context.Accounts.FirstOrDefault(u=>u.Id == uid).Level
                        };
                        #region 保存到Reids中(可异步优化)
                        new RedisHelper.User.UserInfo().InsertUserInfo(uid, new Models.RModels.R_UserInfo()
                        {
                            Avatar = r_UserInfo.Avatar,
                            NickName = r_UserInfo.NickName,
                            Level = r_UserInfo.Level
                        });
                        #endregion
                        return r_UserInfo;
                    }
                }
            }
            else
            {
                return infoR;
            }
        }
    }
}
